﻿using Digitalmes.Model.EquipmentManagement;
using Humanizer.DateTimeHumanizeStrategy;
using Microsoft.EntityFrameworkCore.SqlServer.Query.Internal;
using Opc.Ua.Export;
using System;
using System.Linq;

namespace Digitalmes.Services
{
    public class EquipmentService : BaseService,ISingleton
    {
        public EquipmentService(IServiceProvider serviceProvider) : base(serviceProvider) { }


        ConcurrentDictionary<string,EquipmentParameter> Equipments =new ConcurrentDictionary<string, EquipmentParameter> ();    

        public Task<ServiceResponse> LoadEquipment(EquipmentParameter para)
        {
            var response = new ServiceResponse();
            try
            {
                if(!Equipments.ContainsKey(para.Key))
                {
                    Equipments.TryAdd(para.Key, para);
                }
            
                response.Success = true;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
                response.Exception = ex;

            }
            return Task.FromResult(response);   

 
        }
         


        /// <summary>
        /// 整理驱动分组
        /// </summary>
        /// <returns></returns>
        public async Task<ServiceResponse> LoadNetDriver()
        {
            var response = new ServiceResponse();

            try
            {
                //获取当前驱动分组
                var driveParas = Equipments.Values.GroupBy(x => x.DriverGroup).Select(x => new DriverParameter
                {
                    IpAddress = x.Where(x => !string.IsNullOrEmpty(x.IpAddress)).FirstOrDefault()?.IpAddress,
                    Port = x.First().Port,
                    Tags = x.SelectMany(x => x.Tags).ToList(),
                    Key = x.Key,
                    DriverType=x.First().DriverType,  
                    MonitorTags=x.SelectMany(x => x.MonitorTags).ToList(),
                    Driver=null,
                });
               
                var tasks=new List<Task<ServiceResponse>>();  
                foreach (var drive in driveParas)
                {
                    var tk = Task.Run(async () =>
                    {
                         var ds = GetService<DriverService>();
                         return await ds.LoadDriver(drive);

                    });
                    tasks.Add(tk);
                }

                var results= await Task.WhenAll(tasks);     
               
                response.Success=results.All(x=>x.Success); 
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message=ex.Message;
                response.Exception = ex;    
                
            }
            return response ;
        }



       

        public Task<ServiceResponse> LoadSerialDriver(EquipmentParameter para)
        {
            var response = new ServiceResponse();

            try
            {





                response.Success = true;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
                response.Exception = ex;

            }
            return Task.FromResult(response);
        }


        

    }
}
